package com.itheima.api.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.user.UmsMenuMapper;
import com.itheima.vo.domain.TableDataInfo;
import com.itheima.vo.dto.UmsMenuDto;
import com.itheima.vo.pojo.UmsMenu;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;


@DubboService
public class UmsMenuServiceImpl extends ServiceImpl<UmsMenuMapper, UmsMenu>  implements  UmsMenuService {


    /*
     * 分页查询后台菜单
     * */

    @Autowired
    private   UmsMenuMapper umsMenuMapper;

    @Override
    public TableDataInfo queryPage(Integer pageNum, Integer pageSize, Integer parentId) {
        Page<UmsMenu> page = new Page<>();
        TableDataInfo tableDataInfo = new TableDataInfo();
        LambdaQueryWrapper<UmsMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsMenu::getParentId,parentId);
        umsMenuMapper.selectPage(page,wrapper);
        List<UmsMenu> list = page.getRecords();
        for (UmsMenu menu : list) {
            menu.setCreateTime(LocalDateTime.now());
        }
        tableDataInfo.setList(list);
        tableDataInfo.setPageNum(pageNum);
        tableDataInfo.setPageSize(pageSize);
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setTotalPage(page.getPages());
        return tableDataInfo;
    }

    /*
     * 修改菜单显示状态
     * */
    @Override
    public void updateHidden(UmsMenu umsMenu) {

        umsMenuMapper.updateById(umsMenu);
    }

    @Override
    public List<UmsMenuDto> treeList() {
        LambdaQueryWrapper<UmsMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsMenu::getParentId,0);
        //查询一级所有菜单列表
        List<UmsMenu> list = this.list(queryWrapper);

        List<UmsMenuDto> dto = list.stream().map(item->{
            UmsMenuDto umsMenuDto = new UmsMenuDto();
            BeanUtils.copyProperties(item,umsMenuDto);

            //设置递归
            Children(umsMenuDto);

            return umsMenuDto;
        }).collect(Collectors.toList());
        return dto;
    }

    private void Children(UmsMenuDto umsMenuDto1) {
        //判断当前菜单是否有子菜单'
        LambdaQueryWrapper<UmsMenu> lqw=new LambdaQueryWrapper<>();
        lqw.eq(UmsMenu::getParentId,umsMenuDto1.getId());
        List<UmsMenu> list = this.list(lqw);
        if (list.isEmpty()){
            return;
        }
        List<UmsMenuDto> dtoList=list.stream().map((menu)->{
            UmsMenuDto umsMenuDto = new UmsMenuDto();
            BeanUtils.copyProperties(menu,umsMenuDto);

            Children(umsMenuDto);

            return umsMenuDto;
        }).collect(Collectors.toList());
        umsMenuDto1.setChildren(dtoList);
    }

}
